package com.gexiaobao.pigeon.ui.home.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.PigeonBean
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityTrainingPigeonListBinding
import com.gexiaobao.pigeon.ui.adapter.AddTrainingFlyAdapter
import com.gexiaobao.pigeon.viewmodel.MyTrainingViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/12/26 16:24
 * @Describe    : 训放赛鸽列表（gps）
 */
class ActivityTrainingPigeonList : BaseActivity<MyTrainingViewModel, ActivityTrainingPigeonListBinding>() {

    private lateinit var loadsir: LoadService<Any>
    private val mAdapter: AddTrainingFlyAdapter by lazy { AddTrainingFlyAdapter(arrayListOf(), true) }
    private var muSelectedList = ArrayList<PigeonBean.RingInfo>()
    private var pigeonDataList: MutableList<PigeonBean.RingInfo> = arrayListOf()
    private var pigeonRawList: MutableList<PigeonBean.RingInfo> = arrayListOf()
    private var gpsSns: ArrayList<String> = arrayListOf()
    private var mTrainingId = 0

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "GPS轨迹"
        mDatabind.includeSearch.editQuery.hint = "输入足环"
        mDatabind.includeSearch.editQuery.setHintTextColor(ContextCompat.getColor(this, R.color.comm_text_c3c3c3))

        mTrainingId = intent.getIntExtra("trainingId", 0)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(this, 5f))
        )

        mDatabind.includeList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getPigeonList(true, "1")
        }

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getPigeonList(true, "1")
        }

        loadsir.showLoading()
        mViewModel.getPigeonList(true, "1")

        mAdapter.setOnMultipleChoiceClickListener(object : AddTrainingFlyAdapter.OnItemClickListener {
            override fun onItemClick(position: Int, list: List<PigeonBean.RingInfo>) {
                /** 保存点击item选中的条目 */
                muSelectedList.clear()
                muSelectedList.addAll(list)
            }
        })

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    hideSoftKeyboard(this)
                    searchRingPigeon()
                }

                else -> {}
            }
            false
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun searchRingPigeon() {
        val query = mDatabind.includeSearch.editQuery.text.toString().trim()
        val result = pigeonDataList.filter {
            it.ringId.contains(query, ignoreCase = true)
        }
        mAdapter.setList(result)
        mAdapter.notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.tvLookGpsTrack, mDatabind.includeSearch.tvSearch, mDatabind.includeSearch.ivClose) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
                mDatabind.tvLookGpsTrack -> lookGPSTrack()
                mDatabind.includeSearch.tvSearch -> searchRingPigeon()
                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    mAdapter.setList(pigeonRawList)
                    mAdapter.notifyDataSetChanged()
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取我的赛鸽 */
        mViewModel.pigeonData.observe(this) {
            pigeonDataList.clear()
            pigeonDataList.addAll(it.listData!!)
            loadListData(it, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
            pigeonRawList.clear()
            pigeonRawList.addAll(it.listData)
        }
    }

    private fun lookGPSTrack() {
        if (muSelectedList.isEmpty()) {
            RxToast.showToast("未选择要查看的赛鸽")
            return
        }
        if (muSelectedList.isNotEmpty()) {
            gpsSns.clear()
            muSelectedList.forEach {
                gpsSns.add(it.ringSn)
            }
        }
        startActivity<ActivityGPSMapTrack>("trainingId" to mTrainingId, "gpsSns" to gpsSns)
    }

}